package com.gxa.controller;

import com.gxa.annotation.LoginLog;
import com.gxa.dto.ResultDto;
import com.gxa.pojo.User;
import com.gxa.service.impl.LoginServiceByNameImpl;
import com.gxa.service.impl.LoginServiceByPhoneImpl;
import com.gxa.util.Response;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    @Resource(name = "loginByName")
    private LoginServiceByNameImpl loginServiceByName;

    @Resource(name = "loginByPhone")
    private LoginServiceByPhoneImpl loginServiceByPhone;

    /**
     * 用户名登录页面
     * @return
     */
    @GetMapping("/login/name/page")
    public String loginNamePage(){
        return "loginName";
    }

    /**
     * 手机号登录页面
     * @return
     */
    @GetMapping("/login/phone/page")
    public String loginPhonePage(){
        return "loginPhone";
    }

    /**
     * 手机号登录
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/login/phone/do")
    @ResponseBody
    @LoginLog
    public ResultDto loginPhone(User user, HttpSession session){
        return loginServiceByPhone.login(user,session);
    }
    /**
     * 登录操作-通过用户名
     * @param user
     * @return
     */
    @LoginLog
    @PostMapping("/login/name/do")
    @ResponseBody
    public ResultDto loginName(User user, HttpSession session){
        return loginServiceByName.login(user, session);
    }
    /**
     * 登出操作
     * @return
     */
    @PostMapping("/login/out")
    @ResponseBody
    public ResultDto out(HttpSession session){
        session.removeAttribute("userName");
        return Response.success();
    }
}
